package com.yanqu.road.server.cmd.activity.liondance;

import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.LionDanceProto;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.Command;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.liondance.LionDanceModule;
import com.yanqu.road.server.manger.activity.liondance.LionDanceMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;
import io.netty.channel.ChannelHandlerContext;

@Cmd(code = Protocol.S_LION_DANCE_GIVE_FRIEND_FROM_CROSS, desc = "赠送")
public class LionDanceApplyHandleFromCrossCmd implements Command {

    @Override
    public void execute(ChannelHandlerContext ctx, YanQuMessage yanQuMessage) throws Exception {

        LionDanceProto.CrossLionDanceApplyHandleReqMsg reqMsg = LionDanceProto.CrossLionDanceApplyHandleReqMsg.parseFrom(yanQuMessage.getBytes());

        UserBaseInfo userBaseInfo = PlayerBasePb.parseToUserBaseInfo(reqMsg.getBaseInfo());
        int ret = LionDanceMgr.giveCard(reqMsg.getFromUserId(), reqMsg.getToUserId(), reqMsg.getApplyNo(), reqMsg.getCardId(), userBaseInfo);
        if(ret == 0){
            GamePlayer player = GamePlayerMgr.getOnlinePlayer(reqMsg.getToUserId());
            if(player != null){
                player.getModule(LionDanceModule.class).syncUserData();
            }
        }
        LionDanceProto.CrossLionDanceApplyHandleRespMsg.Builder respMsg = LionDanceProto.CrossLionDanceApplyHandleRespMsg.newBuilder();
        respMsg.setRet(ret);
        respMsg.setActivityId(LionDanceMgr.getActivityId());
        respMsg.setToUserId(reqMsg.getToUserId());
        respMsg.setFromUserId(reqMsg.getFromUserId());
        respMsg.setFromServerId(reqMsg.getFromServerId());
        respMsg.setCardId(reqMsg.getCardId());
        respMsg.setApplyNo(reqMsg.getApplyNo());
        GamePlayerMgr.sendPacket(0, YanQuMessageUtils.buildMessage(Protocol.C_LION_DANCE_GIVE_APPLY_CARD_BACK, respMsg));
    }
}
